package ra_interface;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.security.KeyStore;
import java.security.KeyStoreException;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;


public class SSLConnection {
	private SSLSocket socket = null;
	private BufferedReader bufferedReader = null;
	private BufferedWriter bufferedWriter = null;
	private String ip;
	private int port;
	private boolean isConnected;

	public SSLConnection (String ip, int port) {
		this.ip = ip;
		this.port = port;
		isConnected = false;
	}

	public SSLConnection (SSLSocket acceptedSocket)
	{
		socket = acceptedSocket;
		OutputStream outputStream;
		try {
			outputStream = socket.getOutputStream();
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
			bufferedWriter = new BufferedWriter(outputStreamWriter);

			InputStream inputStream = socket.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			bufferedReader = new BufferedReader(inputStreamReader);
			isConnected = true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isConnected = false;
		}	
	}

	public void connect() throws Exception {
		if (isConnected == false) {
			KeyStore ks = KeyStore.getInstance("JKS");
			FileInputStream fis = null;

			TrustManager[] trustAll = new javax.net.ssl.TrustManager[]{
					new javax.net.ssl.X509TrustManager(){
						public java.security.cert.X509Certificate[] getAcceptedIssuers(){
							return null;
						}
						public void checkClientTrusted(java.security.cert.X509Certificate[] certs,String authType){}
						public void checkServerTrusted(java.security.cert.X509Certificate[] certs,String authType){}
					}
			};

			try {
				fis = new FileInputStream("hieDataStore.crt");
				ks.load(fis, "1q2w3e4r".toCharArray());
			} finally {
				if (fis != null) try {fis.close();} catch(Exception e) {}
			}

			KeyManagerFactory kmf = null;

			if(System.getProperty("java.vm.vendor").toUpperCase().indexOf("IBM") != -1) {
				// IBM JVM Algorithm
				kmf = KeyManagerFactory.getInstance("ibmX509");
			} else {
				// SUN JVM Algorithm
				kmf = KeyManagerFactory.getInstance("SunX509");
			}

			kmf.init(ks, "1q2w3e4r".toCharArray());

			SSLContext ctx = SSLContext.getInstance("SSL");
			ctx.init(kmf.getKeyManagers(), trustAll, new java.security.SecureRandom());


			SSLSocketFactory sslsocketfactory = (SSLSocketFactory) ctx.getSocketFactory();
			socket = (SSLSocket) sslsocketfactory.createSocket(ip, port);
			socket.setEnabledCipherSuites(sslsocketfactory.getDefaultCipherSuites());

			OutputStream outputStream = socket.getOutputStream();
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
			bufferedWriter = new BufferedWriter(outputStreamWriter);

			InputStream inputStream = socket.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			bufferedReader = new BufferedReader(inputStreamReader);
		}
	}

	public void close() throws Exception {
		if (socket != null) socket.close();
	}

	public String readLine() throws IOException {
		String message;
		message = bufferedReader.readLine();
		return message;
	}

	public void writeLine(String message) throws IOException {
		bufferedWriter.write(message + '\n');
		bufferedWriter.flush();
	}
}